import axios from "axios";
import { useState } from "react";
import { useCtx } from "./ctx";
import { useSetState } from "ahooks";

export function useServerData<T = any>(): [T, (val: T) => void] {
    return useSetState<T>(Reflect.get(window, 'SERVER_DATA') || {})
}

export function useQueryOne<T>(sql: string, variables: any[] = []) {
    return useAxiosWebPost('imeepos/core/rest/query', { sql, variables })
}

export function toWebUrl(path: string, params: any = {}) {
    const [s, m, c, a, d] = path.split('/')
    const str = Object.keys(params).map(key => `${key}=${params[key]}`).join('&')
    return `/${s}/${m}/web/${[c, a, d].filter(it => !!it).join('/')}?${str}`
}
export function useAxiosWebPost<O = any, T = any>(path: string, body?: any, parse?: (params: O) => any) {
    const [s, m, c, a, d] = path.split('/')
    const [data, setData] = useState<T>()
    const [loading, setLoading] = useState<boolean>(false)
    const { message } = useCtx()
    const [serverData] = useServerData()
    const fetch = async (params?: O) => {
        setLoading(true)
        return axios.post(`/${s}/${m}/web/${[c, a, d].filter(it => !!it).join('/')}`, parse ? parse(params || body) : params || body, {
            withCredentials: true,
            headers: {},
            params: {}
        }).then(res => res.data).then(res => {
            setLoading(false)
            if (!res) return;
            if (message && res.success === false) {
                message.error(res.message)
            }
            setData(res)
            return res;
        }).catch(async e => {
            setLoading(false)
            throw e;
        })
    }
    return {
        data,
        fetch,
        loading,
        setLoading
    }
}
export const useAxiosPost = <O = any, T = any>(path: string, tip: boolean = true, body?: any, parse?: (params: O) => any) => {
    const [s, m, c, a, d] = path.split('/')
    const [data, setData] = useState<T>()
    const [loading, setLoading] = useState<boolean>(false)
    const { message } = useCtx()
    const fetch = async (params?: O) => {
        setLoading(true)
        return axios.post(`/${s}/${m}/${[c, a, d].filter(it => !!it).join('/')}`, parse ? parse(params || body) : params || body, {
            withCredentials: true,
            headers: {}
        }).then(res => res.data).then(res => {
            setLoading(false)
            if (tip && message?.error && res?.success === false) {
                message?.error(res.message)
            }
            setData(res)
            return res;
        }).catch(async e => {
            setLoading(false)
            throw e;
        })
    }
    return {
        data,
        fetch,
        loading
    }
}
export const useAxiosGet = <O = any, T = any>(path: string, tip: boolean = true, body?: any, parse?: (params: O) => any) => {
    const [s, m, c, a, d] = path.split('/')
    const [data, setData] = useState<T>()
    const [loading, setLoading] = useState<boolean>(false)
    const { message } = useCtx()
    const fetch = async (params?: O) => {
        setLoading(true)
        return axios.get(`/${s}/${m}/${[c, a, d].filter(it => !!it).join('/')}`, {
            params: {
                ...parse ? parse(params || body) : params || body
            },
            withCredentials: true,
            headers: {}
        }).then(res => res.data).then(res => {
            setLoading(false)
            if (tip && message?.error && res?.success === false) {
                message?.error(res.message)
            }
            setData(res)
            return res;
        }).catch(async e => {
            setLoading(false)
            throw e;
        })
    }
    return {
        data,
        fetch,
        loading
    }
}
export function useAxiosWebGet<O = any, T = any>(path: string, tip: boolean = true, body?: any, parse?: (params: O) => any) {
    const [s, m, c, a, d] = path.split('/')
    const [data, setData] = useState<T>()
    const [loading, setLoading] = useState<boolean>(false)
    const { message } = useCtx()
    const fetch = async (params?: O) => {
        setLoading(true)
        return axios.get(`/${s}/${m}/web/${[c, a, d].filter(it => !!it).join('/')}`, {
            params: {
                ...parse ? parse(params || body) : params || body,
            },
            withCredentials: true,
            headers: {}
        }).then(res => res.data).then(res => {
            setLoading(false)
            if (tip && message?.error && res?.success === false) {
                message?.error(res.message)
            }
            setData(res)
            return res;
        }).catch(async e => {
            setLoading(false)
            throw e;
        })
    }
    return {
        data,
        fetch,
        loading
    }
}